var ns;
var app = new Vue({
    el: '#app',
    data: {
        ID: 0,
        list: [],
        tagList: [],
        tagOwnerID: 0,
        show_cover: false,
        editIsShow:false,
    },
    methods: {
        edit: function (item) {
            var that = this;
            ns.openWin('userinfo_frm', '住户信息编辑', {
                id: item.id,
            });

        },
        tel_phone: function (phonenumber) {
            api.call({
                type: 'tel_prompt',
                number: phonenumber
            });
        },

        get_data: function (data, op) {
            var that = this;
            ns.post({
                RoomID: that.ID,
                action: 'getrelateownerlistbyroom'
            }, function (succeed, data, err) {
                // console.log(JSON.stringify(data));
                if (succeed) {
                    that.list = data.list;
                    that.editIsShow=false;
                    if(data.CanEditOwnerSource){
                        that.editIsShow=data.CanEditOwnerSource;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        do_select_tag: function(item) {
            item.isSelected = !item.isSelected;
        },
        do_edit_tag: function (item) {
            var that = this;
            that.tagOwnerID = item.id;
            that.show_cover = true;
            ns.post({
                OwnerID: item.id,
                action: 'gettaglistbyownerid'
            }, function (succeed, data, err) {
                if (succeed) {
                    that.tagList = data;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        saveTag: function () {
            var that = this;
            that.show_cover = true;
            var tagIdList = [];
            that.tagList.forEach(function (item) {
                if (item.isSelected) {
                    tagIdList.push(item.id);
                }
            })
            ns.post({
                OwnerID: that.tagOwnerID,
                tagIds: JSON.stringify(tagIdList),
                action: 'savetagbyowner'
            }, function (succeed, data, err) {
                if (succeed) {
                    that.show_cover = false;
                    api.toast({
                        msg: '保存成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();

                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },
        do_close_tag: function () {
            var that = this;
            that.tagOwnerID = 0;
            that.show_cover = false;
        }
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    if (api.pageParam.id) {
        app.ID = api.pageParam.id;
        app.get_data();
    }

    api.addEventListener({
        name: 'update_ueseinfo'
    }, function (ret, err) {
        if (ret) {
            app.get_data();
        }
    });


}
